-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Report nullable ctor warnings for struct primary constructors #74844
Report nullable ctor warnings for struct primary constructors #74844
Conversation
if (method is SynthesizedPrimaryConstructor primaryCtor && method.ContainingType.IsStructType()) | ||
{ | ||
body = BoundBlock.SynthesizedNoLocals(primaryCtor.GetSyntax()); | ||
nullableInitialState = getInitializerState(body); | ||
} | ||
else if (method is SourceMemberMethodSymbol sourceMethod) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This path was causing us to miss nullable analyzing the struct primary constructor.
@@ -639,8 +639,7 @@ void enforceMemberNotNull(SyntaxNode? syntaxOpt, LocalState state) | |||
Debug.Assert(thisParameter is object); | |||
thisSlot = GetOrCreateSlot(thisParameter); | |||
} | |||
// https://github.com/dotnet/roslyn/issues/46718: give diagnostics on return points, not constructor signature | |||
var exitLocation = method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation(); | |||
var exitLocation = method is SynthesizedPrimaryConstructor || method.DeclaringSyntaxReferences.IsEmpty ? null : method.TryGetFirstLocation(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a type has a primary constructor, all other instance constructors will be required to chain to it (directly or indirectly), so it will be the only constructor which will get warnings for uninitialized non-nullable instance fields. That means the field/property declaration is really the best place to report diagnostics in such cases.
@dotnet/roslyn-compiler for review |
@@ -1735,12 +1735,7 @@ private static void GetStateMachineSlotDebugInfo( | |||
|
|||
initializersBody ??= GetSynthesizedEmptyBody(method); | |||
|
|||
if (method is SynthesizedPrimaryConstructor primaryCtor && method.ContainingType.IsStructType()) | |||
{ | |||
body = BoundBlock.SynthesizedNoLocals(primaryCtor.GetSyntax()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What replicates this logic now? What body is created for the primary constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same logic which was used for class primary constructors is now also used for struct primary constructors. You can see that where the assertions were adjusted.
I reviewed the code around this area and didn't spot anything which would be problematic for structs, but, it's quite possible I missed something.
Closes #74726